class MessageManager {
    constructor(options) {
      const optionNames = Object.getOwnPropertyNames(options);
      optionNames.forEach((name) => {
        this[name] = options[name];
      });
    }
  
    waitPostMessageList = [];
    cacheList = [];
    isPosting = false;
    isError = false;
    interalFlag = null;
  
    push = (message) => {
      console.log("push。。。。。" , new Date(),message)
      if (this.isPosting || this.isError) {
          //没有做定时
        this.cacheList.push(message);
      } else {
        this.waitPostMessageList.push(message);
        if (this.waitPostMessageList.length < this.oncePostLength) {
          if (this.interalFlag === null) {
            this.setIntervalAction();
          }
        } else {
          this.post();
        }
      }
    };
  
    setIntervalAction = () => {
      this.interalFlag = setTimeout(() => {
        if (this.waitPostMessageList.length && !this.isPosting) {
          this.post(true);
        }
        clearTimeout(this.interalFlag);
        this.interalFlag = null;
      }, this.interval);
    };
  
    post = (timeOver) => {
      if (this.isError || this.isPosting) {
        return;
      }
  
      if (!timeOver) {
        if (this.waitPostMessageList.length < this.oncePostLength) {
          console.log(2222222)
          return;
        }
      }
  
      const postingMsgList = this.waitPostMessageList.slice(
        0,
        this.oncePostLength
      );
      this.isPosting = true;
      console.log("发起请求。。。", new Date())
      fetch(this.url, {
        method: "post",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          data: postingMsgList,
        }),
      })
        .then((response) => {
          console.log("请求返回。。。", new Date())
          if (response.status === 500) {
            throw new Error("服务故障");
          }
          return response.json();
        })
        .then((response) => {
          this.isPosting = false;
          this.callback(response, postingMsgList);
          this.waitPostMessageList.splice(0, postingMsgList.length);
          this.waitPostMessageList.push(...this.cacheList);
          this.cacheList = [];
          const logInfo = {
            hasSaveCount: response,
            waitingForSaveCount: this.waitPostMessageList.length,
            currentCount: postingMsgList.length,
          };
          this.log(logInfo);
          if (
            this.waitPostMessageList.length &&
            (this.waitPostMessageList.length >= this.oncePostLength ||
              this.interalFlag === null)
          ) {
            if (this.interalFlag === null) {
              console.log(33333)
              this.post(true);
            } else {
              console.log(1111111)
              this.post();
            }
          }
        })
        .catch((error) => {
          this.isPosting = false;
          this.isError = true;
          const logInfo = {
            errMsg: error,
            waitingForSaveCount:
              this.waitPostMessageList.length + this.cacheList.length,
            currentCount: postingMsgList.length,
          };
          this.log(logInfo);
          this.errorHandle(error, postingMsgList, [
            ...this.waitPostMessageList,
            ...this.cacheList,
          ]);
        });
    };
  
    callback = () => {};
  
    errorHandle = (response, postingMsgList, waitPostMessageList) => {
      console.log(response, postingMsgList, waitPostMessageList);
    };
  
    log = (logContent) => {
      console.log(logContent);
    };
  
    action = () => {
      
    }
  }
  
  export default class MsgManagerBuilder {
    static init = (options) => {
      const msgManager = new MessageManager(options);
      return msgManager;
    };
  }
  